<?php

defined('BASEPATH') OR exit('No direct script access allowed');

include_once ( APPPATH . "libraries/DbPdo.php" );
include_once ( APPPATH . "libraries/enums.php" );

class MY_Controller extends CI_Controller 
{
	protected $viewData;
	protected $isEnglish;
	protected $loggedCustomer;
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
		$this->load->helper('url');
		$this->load->driver('session');
		$this->load->helper('language'); 
		$this->initData();
		$this->lang->load("site", $this->isEnglish ? "english" : "slovak");
	}
	
	private function initData()
	{
		$_isEnglish = $this->session->userdata('isEnglish');
		if (isset($_isEnglish))
			$this->isEnglish = $_isEnglish;
		else
			$this->isEnglish = false;
		
		$_loggedCustomer = $this->session->userdata('loggedCustomer');
		if (isset($_loggedCustomer))
			$this->loggedCustomer = unserialize($_loggedCustomer);
		else
			$this->loggedCustomer = null;
	}
	
	protected function setEnglish($isEnglish)
	{
		$_isEnglish = $this->session->userdata('isEnglish');
		if (isset($_isEnglish) && $_isEnglish == $isEnglish)
			return;
		
		$this->session->set_userdata('isEnglish', $isEnglish); 
	}
	
	private function addDataToView(&$data)
	{
		$data["isLogged"] = false;
		$data["isEnglish"] = false;
		$data["baseUrl"] = base_url();
	}
	public function loadView($viewName, $data)
	{
		$this->addDataToView($data);
		return $this->load->view($viewName, $data, TRUE);
	}
	
	public function showView($viewName, $data = null)
	{
		if ($data == null)
			$data = array();
		
		$this->addDataToView($data);
		$data["menuData"] = $this->loadView("menu", $data);
		$data["centralViewData"] = $this->loadView($viewName, $data);
		$this->load->view("whole_site", $data);
	}
	
	public function displayError($errorString)
	{
		$data["errorString"] = $errorString;
		$this->showView("errorpage", $data);
	}
}
